﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace FreeSwitch.EventSocket
{
	/// <summary>
	/// Used to inject dependencies
	/// </summary>
	public interface IDependencyInjector
	{
		T Resolve<T>() where T : class, new();
	}

	public class ComponentAttribute : Attribute
	{
		public ComponentFlags Flags { get; set; }

		public ComponentAttribute(ComponentFlags flags)
		{
			Flags = flags;
		}

		public ComponentAttribute()
		{}
	}

	[Flags]
	public enum ComponentFlags
	{
		Singleton = 1
	}

/*
	public class TinyIOC
	{
		Dictionary<Type, LinkedList<Component>> _items = new Dictionary<Type, LinkedList<Component>>();

		public void Register<TContract, TImplementation>()
		{
			var components = Get(typeof (TContract)) ?? new LinkedList<Component>();
			components.AddLast(new Component
			                   	{
			                   		ImplementationType = typeof (TImplementation),
			                   		IsSingleton = true,
			                   		ServiceType = typeof (TContract)
			                   	});
			_items[typeof (TContract)] = components;
		}

		private LinkedList<Component> Get(Type type)
		{
			LinkedList<Component> items;
			return _items.TryGetValue(type, out items) ? items : null;
		}

		public T Resolve<T>()
		{
			var item = Resolve(typeof (T));
			if (item is LinkedList<Component>)
				throw new 
			return (T)Resolve(typeof(T));
		}


		public object Resolve(Type contract)
		{
			var components = _items[contract];
			ConstructorInfo constructor = implementation.GetConstructors()[0];

			ParameterInfo[] constructorParameters = constructor.GetParameters();

			if (constructorParameters.Length == 0)
				return Activator.CreateInstance(implementation);

			List<object> parameters = new List<object>(constructorParameters.Length);

			foreach (ParameterInfo parameterInfo in constructorParameters)
				parameters.Add(Resolve(parameterInfo.ParameterType));

			return constructor.Invoke(parameters.ToArray());
		}
	}

	 

	class Component
	{
		/// <summary>
		/// Gets or sets service type.
		/// </summary>
		public Type ServiceType { get; set; }

		/// <summary>
		/// Gets or sets implementation type.
		/// </summary>
		public Type ImplementationType { get; set; }

		/// <summary>
		/// Gets or sets if component is a singleton.
		/// </summary>
		public bool IsSingleton { get; set; }

		/// <summary>
		/// Gets or sets created instance.
		/// </summary>
		public object Instance { get; set; }
	}
	
*/	
}
